import struct

def pack_query_cmd(obj, mode, vo=0, vs=0, ve=0, t=0, v=0):
    # obj: 0x01(Vg) or 0x02(Vd)
    # mode: 0x01(扫描) or 0x02(定值)
    data = [0x55, 0xA1, obj, mode]
    if mode == 0x02:  # 定值
        vo_abs = abs(vo)
        vo_low = vo_abs & 0xFF
        vo_high = ((vo_abs >> 8) & 0x7F) | (0x80 if vo < 0 else 0x00)
        data += [vo_low, vo_high]
        data += [0, 0, 0, 0, 0, 0]
    else:  # 扫描
        # 起始值负号处理
        vs_abs = abs(vs)
        ve_abs = abs(ve)

        vs_low = vs_abs & 0xFF
        vs_high = ((vs_abs >> 8) & 0x7F) | (0x80 if vs < 0 else 0x00)

        ve_low = ve_abs & 0xFF
        ve_high = ((ve_abs >> 8) & 0x7F) | (0x80 if ve < 0 else 0x00)

        data += [0, 0]
        data += [vs_low, vs_high]
        data += [ve_low, ve_high]
        data += [t & 0xFF, (t >> 8) & 0xFF]
        data += [v & 0xFF, (v >> 8) & 0xFF]
    while len(data) < 15:
        data.append(0x00)
    data.append(0x5C)
    return bytes(data)

def pack_set_cmd(obj):
    data = [0x55, 0xA2, obj]
    while len(data) < 15:
        data.append(0x00)
    data.append(0x5C)
    return bytes(data)

def pack_test_cmd(start, channel_count, channel_order=None):
    # start: 0x01启动, 0x02停止
    # channel_order: 6个字节的启动顺序，默认为[1,2,3,4,5,6]
    data = [0x55, 0xA3, start, channel_count]
    
    # 添加6个字节的启动顺序
    if channel_order is None:
        channel_order = [1, 2, 3, 4, 5, 6]
    
    # 确保channel_order有6个元素
    order_bytes = channel_order[:6] if len(channel_order) >= 6 else channel_order + [0] * (6 - len(channel_order))
    data.extend(order_bytes)
    
    # 补齐到15字节
    while len(data) < 15:
        data.append(0x00)
    data.append(0x5C)
    return bytes(data)

def parse_query_response(data):
    # 解析查询响应
    if len(data) != 16 or data[0] != 0x55 or data[-1] != 0x5C:
        return None
    return {
        'status': data[2],  # 0x01成功, 0x00失败
    }

def parse_set_response(data):
    # 解析设置响应
    if len(data) != 16 or data[0] != 0x55 or data[-1] != 0x5C:
        return None
    obj = data[2]
    mode = data[3]
    vo = data[5] * 256 + (data[4] & 0x7F)
    if data[4] & 0x80:
        vo = -vo

    vs = data[7] * 256 + (data[6] & 0x7F)
    if data[6] & 0x80:
        vs = -vs

    ve = data[9] * 256 +(data[8] & 0x7F)
    if data[8] & 0x80:
        ve = -ve

    t = data[11] * 256 + data[10]
    v = data[13] * 256 + data[12]
    return {
        'obj': obj,
        'mode': mode,
        'vo': vo,
        'vs': vs,
        've': ve,
        't': t,
        'v': v
    }

def parse_test_response(data):
    # 解析测试响应
    if len(data) != 16 or data[0] != 0x55 or data[-1] != 0x5C:
        return None
    return {
        'status': data[2],  # 0x01启动, 0x02停止
    }

def parse_data_response(data):
    # 解析A4数据帧
    if len(data) != 16 or data[0] != 0x55 or data[1] != 0xA4 or data[-1] != 0x5C:
        return None
    channel_count = data[2]
    channel_info = data[3]  # 包含通道号和遍数编号的字节
    
    # 解析通道号（低4位）和遍数编号（高4位）
    channel_no = channel_info & 0x0F  # 低4位：通道号
    round_no = (channel_info >> 4)  # 高4位：遍数编号（1-10遍）
    
    total_points = data[4]
    seq = data[5]
    current_bytes = bytes(data[6:10])
    current = struct.unpack('<f', current_bytes)[0]
    return {
        'channel_count': channel_count,
        'channel_no': channel_no,
        'round_no': round_no,  # 新增遍数编号
        'total_points': total_points,
        'seq': seq,
        'current': current
    }